/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hyper.market.beans;

import java.util.ArrayList;
import database.DBAccessController;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Mikeno
 */
public class StoreBean {

    private int storeid, telephone;
    private String address, storeName;

    public StoreBean() {
    }

    public StoreBean(int storeid, String storeName) {
        setStoreid(storeid);
        setStoreName(storeName);
    }

    public StoreBean(int storeid, String storeName, String address, int telephone) {
        setStoreid(storeid);
        setAddress(address);
        setTelephone(telephone);
        setStoreName(storeName);

    }

    public ArrayList retrieveAllStoreDetails(String jndi) {

        DBAccessController db = new DBAccessController(jndi);
        String sql = "select * from store";
        ArrayList storeList = new ArrayList();
        StoreBean store;
        try {
            ResultSet rs = db.execSelectQuery(sql);
            while (rs.next()) {
                store = new StoreBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4));
                storeList.add(store);
            }
            db.terminate();
        } catch (SQLException e) {
            e.printStackTrace();

        }

        return storeList;
    }

    public ArrayList retrieveStoreName(String jndi, ArrayList storeidList) {

        DBAccessController db = new DBAccessController(jndi);
        String sql = "";
        ArrayList storename = new ArrayList();
        for (int i = 0; i < storeidList.size(); i++) {
            try {
                sql = "select storeName from store where storeid=" + storeidList.get(i);
                ResultSet rs = db.execSelectQuery(sql);
                if (rs.next()) {
                    storename.add(rs.getString(1));
                }

            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        db.terminate();
        return storename;
    }

    public ArrayList retrieveAllStoresIdandName(String jndi) {

        DBAccessController db = new DBAccessController(jndi);
        String sql = "";
        ArrayList storeList = new ArrayList();
        try {
            sql = "select storeid, storeName from store";
            ResultSet rs = db.execSelectQuery(sql);
            while (rs.next()) {
                StoreBean store = new StoreBean(rs.getInt("storeid"), rs.getString("storeName"));
                storeList.add(store);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        db.terminate();
        return storeList;
    }


 public void retrieveStoreSales(String jndi, String jndi2) {
        DBAccessController db = new DBAccessController(jndi);
        DBAccessController db2 = new DBAccessController(jndi2);
        String sql = "select * from salestransaction";
        String sql2 = "";
        try {
            ResultSet rs = db.execSelectQuery(sql);
            while(rs.next()) {
                sql2 = "insert into salestransaction (salestransactionid, stockid,dateofsales,quantity,sellingprice,discount, storeid) values('"+rs.getInt("salestransactionid")+"','"+rs.getInt("stockdetailid")+"','"+rs.getDate("DateofSales")+"','"+rs.getInt("Quantity")+"','"+rs.getDouble("Sellingprice")+"','"+rs.getDouble("discount")+"','"+rs.getInt("storeid")+"')";
                db2.execUpdateQuery(sql2);

            }
            db.execUpdateQuery("delete from salesTransaction");
            db.terminate();
            db2.terminate();
        } catch (SQLException e) {
            e.printStackTrace();
            db.terminate();
            db2.terminate();
        }
}

    /**
     * @return the storeid
     */
    public int getStoreid() {
        return storeid;
    }

    /**
     * @param storeid the storeid to set
     */
    public void setStoreid(int storeid) {
        this.storeid = storeid;
    }

    /**
     * @return the telephone
     */
    public int getTelephone() {
        return telephone;
    }

    /**
     * @param telephone the telephone to set
     */
    public void setTelephone(int telephone) {
        this.telephone = telephone;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the storeName
     */
    public String getStoreName() {
        return storeName;
    }

    /**
     * @param storeName the storeName to set
     */
    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }
}
